﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;

namespace MedicWeb.forms
{
    public partial class GridViewDoctorList : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            ////////////////////////////////////
            var mc = new MainService();
            var doctorList = Session["docList"] as List<Doctor>;
            if (doctorList == null)
            {
                doctorList = new List<Doctor>();
                doctorList.AddRange(mc.GetDoctorList(GetSession(), DateTime.Now.Year, false));
                Session["docList"] = doctorList;
            }
            ///////////////////////////////////////////

            var gvdatasource = doctorList.Select(d => new
                                                          {
                                                              d.NumberInOrder,
                                                              d.Surname,
                                                              d.Patronymic,
                                                              d.Name,
                                                              d.Address,
                                                              d.Data,
                                                              Job = d.Job.Title
                                                          });

            GridView1.DataSource = gvdatasource;
            GridView1.DataBind();

            Speciality[] ddldatasource = mc.GetSpecialityList(GetSession(), false);

            ddlSpeciality.DataSource = ddldatasource;
            ddlSpeciality.DataTextField = "Title";
            ddlSpeciality.DataValueField = "id";
            ddlSpeciality.DataBind();

            ddlSpeciality.Items.Insert(0, new ListItem("--Все--", "0"));
            
        }

        protected void ddlSpeciality_SelectedIndexChanged(object sender, EventArgs e)
        {
            ////////////////////////////////////
            var mc = new MainService();
            var doctorList = Session["docList"] as List<Doctor>;
            if (doctorList == null)
            {
                doctorList = new List<Doctor>();
                doctorList.AddRange(mc.GetDoctorList(GetSession(), DateTime.Now.Year, false));
                Session["docList"] = doctorList;
            }
            ///////////////////////////////////////////
            var id = Convert.ToInt32(ddlSpeciality.SelectedItem.Value);

            var gvdatasource = doctorList
                .Where(d => d.NewSpeciality.Id == id)
                .Select(d => new
                {
                    d.NumberInOrder,
                    d.Surname,
                    d.Patronymic,
                    d.Name,
                    d.Address,
                    d.Data,
                    Job = d.Job.Title
                });

            if (id == 0)
            {
                gvdatasource = doctorList
                .Select(d => new
                {
                    d.NumberInOrder,
                    d.Surname,
                    d.Patronymic,
                    d.Name,
                    d.Address,
                    d.Data,
                    Job = d.Job.Title
                });
            }
           

            GridView1.DataSource = gvdatasource;
            GridView1.DataBind();
        }

       

      
    }
}